package com.chenxbook.modules.examples.eventlistener.propagation;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.pvm.internal.model.ExecutionImpl;

import java.util.ArrayList;
import java.util.List;

/**
 *  事件监听必须实现EventListener
 *  <br> 维护流程运行的日志，并保存在流程变量中
 * */
public class LogListener implements EventListener {
    private static final long serialVersionUID = 5512465449605278212L;
    String msg;
    @Override
    public void notify(EventListenerExecution execution) {
        //获取流程变量logs，用来记录跟踪日志
        List<String> logs = (List<String>) execution.getVariable("logs");
        if (CollectionUtils.isEmpty(logs)){
            logs = new ArrayList<String>();
            execution.setVariable("logs",logs);
        }
        //获取活动名称
        String activityName = ((ExecutionImpl) execution).getActivityName();
        //结合流程定义，该事件监听器被定义在流程全局，首先将监听到流程实例的start事件，在此事件中，活动名称（actName）为null
        //利用process替换
        logs.add(msg+(StringUtils.isBlank(activityName)?"process":activityName));
        execution.setVariable("logs",logs);
    }
}
